<template>
	<div class="com-index">
		<!-- 搜索栏 -->
		<!-- <div class="com-index-search" v-if="isSearch">
			<slot name="search">
				<Search :showSearch="showSearch" :breadCrumb="breadCrumb" />
			</slot>
		</div> -->
		<div class="com-index-content">
			<template v-if="isInit">
				<!-- 过滤 -->
				<Filter ref="filterRef" v-if="showFilter" @update="updateFilter" :toggleView="toggleView">
					<slot name="filterForm"></slot>
				</Filter>
				<!-- 卡片和列表 -->
				<div class="list">
					<!-- 初始化 -->
					<template v-if="isInitList">
						<Card :dataList="dataList" :filterList="filterRef?.filterList" v-if="viewType === 0" />
						<slot name="table" :dataList="dataList" v-if="viewType === 1"></slot>
					</template>
					<!-- 自定义 -->
					<template v-else>
						<!-- <slot name="card" v-if="viewType === 0"></slot> -->
						<slot :dataList="dataList"></slot>
					</template>
				</div>
			</template>
			<slot name="content"></slot>
		</div>
		<!-- 新建弹窗 -->
		<slot name="add"></slot>
	</div>
</template>
<script lang="ts" setup>
const props = defineProps({
	getListFun: {
		type: Function,
		default: () => {
			return function () {};
		}
	}, // 数据
	isInitList: {
		type: Boolean,
		default: () => {
			return true;
		}
	}, // 是否使用初始化列表视图
	toggleView: {
		type: Boolean,
		default: () => {
			return true;
		}
	},
	showFilter: {
		type: Boolean,
		default: () => {
			return true;
		}
	},
	showSearch: {
		type: Boolean,
		default: () => {
			return true;
		}
	},
	params: {
		type: Object,
		default: () => {
			return {};
		}
	},
	// 是否初始化执行函数
	initApi: {
		type: Boolean,
		default: true
	},
	keywords: {
		type: String,
		default: () => {
			return "";
		}
	},
	breadCrumb: {
		//面包屑配置
		type: Object,
		default: () => null
	},
	isInit: { type: Boolean, default: () => { return true } },
	isSearch: { type: Boolean, default: () => { return true } },
});
import Search from "./Search.vue";
import Filter from "./Filter.vue";
import mitt from "@/utils/bus";
const emit = defineEmits(["search", "update:keywords"]);
const Card = defineAsyncComponent(() => import("./Card.vue"));
const filterRef = ref(null);
const listRef = ref(null);
const viewType = ref(0); // 视图方式  0阶段 1表格
const dataList = ref([]); // 列表
const isHaveData = ref(false);
const pageParams = ref({
	total: 0,
	pageNo: 1,
	pageSize: 10
});

watch(
	() => props.getListFun,
	(e) => {
		nextTick(() => {
			if (props.initApi) {
				getList();
			}
		});
	},
	{ immediate: true }
);
const updateFilter = (val) => {
	if (!Array.isArray(val)) return;
	const keywordsInfo = val.find((i) => i.filterType === "keywords");
	emit("update:keywords", keywordsInfo?.value);
	emit("search", val);
};
watch(
	() => filterRef?.value?.filterList,
	(val) => {
		if (val?.length > 0) {
			val.forEach((item) => {
				pageParams.value[item.keys] = item.value;
			});
			nextTick(() => {
				getList();
			});
		}
	},
	{ deep: true, immediate: true }
);

const loading = ref(false);
// 获取列表
const getList = async () => {
	dataList.value = [];
	loading.value = true;
	const { pageNo, pageSize } = pageParams.value;
	pageParams.value = {
		pageNo,
		pageSize,
		...filterRef?.value?.filterParams,
		...props.params
		// searchKeys: filterRef?.value?.filterList
	};
	try {
		delete pageParams.value.total;
		const res = await props.getListFun(pageParams.value);
		if (typeof res === "function") return;
		pageParams.value.total = res?.total || 0;
		dataList.value = viewType.value === 0 ? [...dataList.value, ...res] : res?.list || [];
		for (const k of dataList.value) {
			if (k?.list?.total >= 10) {
				isHaveData.value = true;
				break;
			}
		}
	} catch (err) {
		console.log(err);
	} finally {
		loading.value = false;
	}
};

// 获取子组件触发的方法
const getEmit = (val) => {
	console.log("val===", { ...val });
	const { emitType, value, keys } = val;
	if (emitType === "view") {
		// 切换视图
		viewType.value = value;
	}
	if (emitType === "loading") {
		// 切换视图
		if (isHaveData.value) {
			// pageParams.value.pageNo++;
			// getList();
		}
	} else if (emitType === "delete") {
		// 删除过滤条件
		if (value) {
			getList();
		}
	}
};

provide("getEmit", getEmit);
provide("getList", getList);

defineExpose({
	getList,
	filterRef,
	viewType,
	dataList,
	pageParams,
	loading
});
</script>
<style lang="scss" scoped>
// 首页
.com-index {
	height: 100%;
	&-search{
		display: flex;
		align-items: flex-end;
		justify-content: space-between;
		height: var(--header-height) !important;
		padding: 0 15px 7px 15px;
		&>div{
			flex: 1;
		}
		.crumbs{
			height: 24px;
			padding-bottom: 2px;
			display: inline-flex;
			align-items: center;
			justify-content: start;
			color: #999;
			font-weight: bold;
			.EachApp{
				margin-right: 2px;
				font-weight: normal;
				padding: 2px;
				cursor: pointer;
				&.close{
					&:hover{
						background: #ddd;
						border-radius: 3px;
					}
				}
			}
			.initial{
				cursor: initial;
				&:hover{
					background: initial !important;
				}
			}
		}
		.search-box {
			width: 400px;
			flex: initial;
		}
		.btns{
			padding-bottom: 5px;
			text-align: right;
		}
		.EachApp{
			color: #999;
			padding: 4px;
			border-radius: 2px;
			&:hover{
				background: #ddd;
			}
		}
	}
	&-content {
		background: white;
		-moz-box-shadow: -1px 0px 2px rgba(0, 0, 0, 0.00784313725490196);
		-webkit-box-shadow: -1px 0px 2px rgba(0, 0, 0, 0.00784313725490196);
		box-shadow: -1px 0px 2px rgba(0, 0, 0, 0.00784313725490196);
		height: 100%;
		border-radius: 6px;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		&.padding {
			padding: 0;
		}
		.arco-empty {
			padding: 64px 0 !important;
			&-description {
				font-size: 13px !important;
			}
		}
		.list {
			flex: 1;
			display: flex;
			flex-direction: column;
			// height: 100%;
			overflow: auto;
			// padding: 10px 0 0 0;
			// overflow-x: auto;
		}
		// 过滤
		.index-filters {
			padding: 5px 10px;
			.arco-select {
				background-color: initial !important;
				box-shadow: initial !important;
				min-height: 26px;
				padding: 0;
				&.arco-select-view-focus {
					border-color: transparent;
				}
				&:focus-visible {
					border-color: transparent;
				}
			}
			&-sort {
				// border-bottom: 1px solid #eee;
				padding: 5px 0;
				height: 40px;
				position: relative;
				z-index: 9;
				background: white;
				.right {
					display: flex;
					.flex {
						cursor: pointer;
						&:not(:last-child) {
							margin-right: 20px;
						}
					}
				}
			}
			&-item {
				padding: 4px 0;
				border-bottom: 1px solid #eee;
				overflow: hidden;
				background: white;
				max-height: var(--filter-height);
				transition: 0.5s;
				.tag-box {
					display: flex;
					flex-wrap: wrap;
					// gap: 4px;
				}
				.close {
					margin-left: 5px;
					cursor: pointer;
					&:hover {
						color: #999;
					}
				}
				&.height {
					padding: 0;
					border: none;
					max-height: 0;
				}
				.right {
					flex-shrink: 0;
				}
			}


			.btn-list {
				text-align: center;
				.item {
					&:not(:last-child) {
						margin-bottom: 10px;
					}
				}
			}
		}
		// card样式
		.index-card {
			overflow-x: auto;
			&-progress {
				display: flex;
				column-gap: 35px;
				& > .item {
					min-width: 300px;
				}
				.title {
					font-size: 14px;
				}
				.progress {
					padding: 15px 0;
					.arco-progress {
						.arco-progress-line-text {
							min-width: 80px;
							text-align: right;
							font-size: 13px !important;
							font-weight: bold;
							color: #333;
						}
					}
				}
			}
			&-body {
				display: flex;
				column-gap: 35px;
				height: var(--index-body-height);
				width: max-content;
				padding-bottom: 6px;
				overflow-y: scroll;
				&::-webkit-scrollbar {
					display: none;
				}
				&::-moz-scrollbar {
					display: none;
				}
				&::-ms-scrollbar {
					display: none;
				}
				&::-o-scrollbar {
					display: none;
				}
				.item-list {
					min-width: 300px;
					border-radius: 6px;
					border: 1px solid #eee;
					height: max-content;
					&.border {
						border: none;
					}
					& > .item {
						padding: 10px 20px;
						&:hover {
							box-shadow: 0 3px 20px 4px #f2f1f1;
						}
						.title {
							font-weight: bold;
							.arco-input {
								input {
									font-size: 20px;
									color: #333;
								}
							}
						}
						.price {
							height: 50px;
							display: flex;
							align-items: center;
							font-weight: bold;
							font-size: 16px;
							.old {
								color: #ebeef5;
								margin-right: 10px;
							}
						}
						.tags {
							height: 50px;
							display: flex;
							align-items: center;
						}
						&:not(:last-child) {
							border-bottom: 1px solid #f2f1f1;
						}
						.EachApp {
							cursor: pointer;
						}
					}
				}
				&.height {
					height: calc(100vh - 238px);
				}
			}
		}
	}
}

// 新建-展开更多
.index-more {
	text-align: center;
	padding: 20px 0;
	cursor: pointer;
}

</style>
